/**
 *
 * @file-name: cart.js
 *
 *	@auther: wcc
 *
 *	@info: model cart
 *
 **/
$(document).ready( function() {

    //    操作按钮时执行
    $('.operate').bind("click", function() {
        var operate = $(this).text();
        var parent = $(this).parent().parent().parent();
        var price = parent.find('.price');
        var amount = parent.find(".amount");
        var total = parent.find('.showtotal');

        if(operate == "+") {
            amount.attr('value', parseInt(amount.val()) + 1);
        } else if(operate == '—') {
            if(parseInt(amount.val()) < 0) {
                alert(amount.val());
                amount.attr('value', parseInt(amount.val()) - 1);
            }
        }
        total.html( (parseInt(amount.val()) * parseFloat(price.val()) ).toFixed(2) );
        count();
    });
    /***  计算总计  ***/
    function count() {
        var allamountN = 0;
        var alltotalN = 0;
        $('.amount').each(function() {
            allamountN = parseInt(allamountN) + parseInt( $(this).val() );
        });
        $('#allamount').text(allamountN);


        $('.showtotal').each(function() {
            alltotalN = parseFloat(alltotalN) + parseFloat($(this).text());
        });
        $('#alltotal').text(alltotalN.toFixed(2));
    }

    $('.delete').click(function() {
        var id = $(this).parent().find(".hidden-id").val();
        var obj = $(this).parent().parent().parent();//.remove();

        $.get('/EachGo/index.php/Home/ShoppingCart/delCart',
            {id:id},
            function(data){
                obj.remove();
                count();
                console.log(data.info);
            },"json");
    });

    /**       删除全部所选      **/
    $('.delete-all').bind("click",function() {
        var $objs = $('.one:checked');
        var arr = new Array(1);
        for( var i=0; i<$objs.length;i++){
            arr['id'+i] = $objs.get(i).value;
            //arr[i] = $objs.get(i).value;
            $objs.get(i).closest('tr').remove();
        //    todo 需要验证是否正确
        }

        $.get('/EachGo/index.php/Home/ShoppingCart/delCart',
            arr ,
            function(data) {
                count();
                console.log(data.info);
            },"json");
    });


    /**  页面准备好时就执行总计   **/
    $(function() {
        count();
    });
});

$(function(){/*全选&取消全选*/
    $(".all").click(function(){/*prop()对于需要使用一个函数设置多个属性值或是一次性设置多个属性值的情况    1属性值和属性名相同时     2属性值只能为true和false的时候*/
        if($(this).is(":checked")) {
            $(".one").prop("checked",true);
            $(".all").prop("checked",true);
        }
        else if($(this).not(":checked")){
            $(".one").prop("checked",false);
            $(".all").prop("checked",false);
        }
    });
});


 /**
  *
  * @author Mitol
  *
  * @info shopcart fly star
  *
  */
 $(function() {
     var offset_end = $("#end").offset();
     $(".addcar").click(function(event){
         var addcar = $(this);
         var offset_start = $(this).offset();
         var img = addcar.parent().find('img').attr('src');
         var flyer = $('<img class="u-flyer" src="'+img+'" style="display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;">');

         if( offset_start.top > $(window).height()) {
             offset_start.top -= $(window).height();
         }


         flyer.fly({
             start: {
                 left: offset_start.left, //飞星起点#fly 距离左边的 position: fixed
                 top: offset_start.top    //飞星起点 距离右边的
             },
             end: {
                 left: offset_end.left-195,  //终点距离浏览器顶的距离
                 top: 10,    //终点距离浏览器顶的距离
                 width: 20, //飞星消失时候的宽度
                 height: 20 //飞星消失时候的高度
             },
             onEnd: function(){ //飞星结束后执行
                 //alert(offset_start.top + ","+ $(window).height());
                 //$("#msg").show().animate({width: '250px'}, 200).fadeOut(1000);
                 addcar.css({"cursor":"default","background-color":"#CCC"}).unbind('click');
                 addcar.text("已加入购物车");
                 this.destory();
             }
         });
     });
 });